package com.xpc.dateapi.controller;

import com.xpc.dateapi.service.IDateApiService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Classname DateApiController  节假日查询的API
 * 参考 https://gitee.com/web/holidays_api
 * https://tool.bitefu.net/jiari/
 * @Created by xpc
 * @Date 2021/10/18 16:26
 * @version: 1.0
 */
@RestController
public class DateApiController {

    private static final Logger log = LoggerFactory.getLogger(DateApiController.class);

    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Autowired
    private IDateApiService dateApiService;

    /**
     * 节假日查询
     *
     * @param: dates yyyyMMdd格式，要判断多个日期时，中间用,隔开
     * @return: 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
     * @createAuthor: xpc
     * @createDate: 2021-10-18 16:51
     * @version: 1.0
     */
    @GetMapping("/jiari")
    public String jiari(@RequestParam(value = "d") String dates, HttpServletRequest request) {
        // 后面可以通过ip限制访问等操作
        String ipAddr = DateApiController.getIpAddr(request);
        log.info(DateApiController.SDF.format(new Date()) + "\t请求地址ip------>{}===[{}]", ipAddr, dates);
        if (StringUtils.isEmpty(dates)) {
            return "参数d不能为空";
        }
        String ret = "";
        String[] split = dates.split(",");
        int length = split.length;

        if (length == 1) ret = dateApiService.singleDate(dates);
        if (length > 1) ret = dateApiService.moreDate(split);
        return ret;
    }

    /**
     * 获取ip
     *
     * @param: request
     * @return: ip
     * @createAuthor: xpc
     * @createDate: 2021-10-19 11:30
     * @version: 1.0
     */
    private static String getIpAddr(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

}
